import { Controller, Get, Param, Post, UseGuards, Body, ParseIntPipe, Query } from "@nestjs/common";
import { UserService } from "./user.service";
import { User } from "../../ShareModule/entities/user.entity";
import { UserDto, editUserDto } from "./user.dto";
import { AuthGuard } from "@nestjs/passport";
import { ApiBearerAuth, ApiUseTags, ApiImplicitQuery } from "@nestjs/swagger";
import { Roles } from "../../CoreModule/decorator/common.decorator";

@ApiBearerAuth()
@ApiUseTags('user')
@Controller('user')
export class UserController {
    constructor(private readonly UserService: UserService) { }

    @Get()
    @Roles('admin')
    @UseGuards(AuthGuard())
    async findAll(): Promise<User[]> {
        return await this.UserService.findAll();
    }

    @Get('page')
    @Roles('admin')
    @UseGuards(AuthGuard())
    @ApiImplicitQuery({ name: 'pageSize' })
    @ApiImplicitQuery({ name: 'pageNumber' })
    async paginator(@Query() query: any): Promise<object> {
        return await this.UserService.paginator({
            pageSize: Number(query.pageSize),
            pageNumber: Number(query.pageNumber),
        })
    }


    @Get(':id')
    async findOne(@Param('id', new ParseIntPipe()) id: number): Promise<User> {
        return await this.UserService.findOne({ where: id });
    }

    @Post('create')
    @Roles('admin')
    @UseGuards(AuthGuard())
    async create(@Body() user: UserDto): Promise<User> {
        return await this.UserService.create(user);
    }

    @Post('edit')
    @Roles('admin')
    @UseGuards(AuthGuard())
    async edit(@Body() user: editUserDto): Promise<string> {
        return await this.UserService.edit(user.id, user);
    }

    @Get('remove/:id')
    @Roles('admin')
    @UseGuards(AuthGuard())
    async remove(@Param('id', new ParseIntPipe()) id: number): Promise<string> {
        return await this.UserService.delete(id);
    }
}